package work.parabola.keyboardswitch.pojo.query.storeswitch;

import lombok.*;
import org.apache.commons.lang3.StringUtils;
import work.parabola.keyboardswitch.common.ServerResponse;

@Data
@ToString
@EqualsAndHashCode
public class ListQuery {

    private String searchWord;

    private Integer switchType;

    private Boolean amountDescOrder;

    private Boolean priceDescOrder;

    private String orderByAmount;

    private String orderByPrice;

    public ServerResponse validate() {
        if (StringUtils.isBlank(this.searchWord)) {
            this.searchWord = null;
        } else {
            this.searchWord = "%" + this.searchWord + "%";
        }
        if (StringUtils.isBlank(this.orderByAmount)) {
            this.orderByAmount = null;
            this.amountDescOrder = null;
        } else {
            if (Order.codeOf(orderByAmount) == null) {
                return ServerResponse.createByErrorMessage("按数量排序参数错误");
            }
            if (Order.codeOf(this.orderByAmount).equals(Order.DESC)) {
                this.amountDescOrder = true;
            } else {
                this.amountDescOrder = false;
            }

        }
        if (StringUtils.isBlank(this.orderByPrice)) {
            this.orderByPrice = null;
            this.priceDescOrder = null;
        } else {
            if (Order.codeOf(this.orderByPrice) == null) {
                return ServerResponse.createByErrorMessage("按价格排序参数错误");
            }
            if (Order.codeOf(this.orderByPrice).equals(Order.DESC)) {
                this.priceDescOrder = true;
            } else {
                this.priceDescOrder = false;
            }
        }
        if (this.switchType == null || this.switchType == -1) {
            this.switchType = null;
        }
        return ServerResponse.createBySuccess();
    }

    @AllArgsConstructor
    @Getter
    public enum Order {
        DESC("descending"),
        ASC("ascending");
        String desc;

        public static Order codeOf(String desc) {
            for (Order order : values()) {
                if (order.getDesc().equals(desc)) {
                    return order;
                }
            }
            return null;
        }
    }


}
